#!/bin/bash

OBJ_PATH=`pwd`
INSTALL_PATH=${OBJ_PATH}/install
QEMU_INSTALL=${OBJ_PATH}/qemu_install
SRC_PATH=${OBJ_PATH}/src

AARCH64_CONFIG="${SRC_PATH}/aarch64_config"
RISCV64_CONFIG="${SRC_PATH}/riscv64_config"
RCS_FILE="${OBJ_PATH}/src/rcS"

QEMU=qemu-6.0.0.tar.bz2
QEMU_DIR=${OBJ_PATH}/qemu-6.0.0
BUSYBOX=busybox-1.31.0.tar.bz2
LOCAL_BUSYBOX=busybox-1.31.0-fix.tar.bz2
BUSYBOX_DIR=${OBJ_PATH}/busybox-1.31.0
ARCH=$1

#
# 1. Download qemu & Compile
#
qemu_install()
{
	if [ ! -f ${QEMU} ]; then
		wget https://download.qemu.org/${QEMU}
		if [ $? -ne 0 ]; then
			echo "wget https://download.qemu.org/qemu-6.0.0.tar.bz2 fail, check machine network Please."
			echo "Or download qemu file(${QEMU}) to ${OBJ_PATH}"
			exit
		fi
	fi
	
	tar -xvf ${QEMU}
	cd ${QEMU_DIR}
	
	./configure --target-list=riscv64-softmmu,aarch64-softmmu --prefix=${QEMU_INSTALL}
	make -j32
	make install
}

#
# 2. Download busy & Compile
#
init_filesystem()
{
	cd _install

	mkdir -p etc/init.d/
	cp ${RCS_FILE} etc/init.d/
	chmod +x etc/init.d/${RCS_FILE}

	cd -
}

busybox_install()
{
	cd ${OBJ_PATH}

	if [ ! -f ${BUSYBOX} ]; then
		wget https://busybox.net/downloads/${BUSYBOX}
		if [ $? -ne 0 ]; then
			echo "wget https://download.qemu.org/${BUSYBOX} fail, check machine network Please."
			echo "Or download qemu file(${BUSYBOX}) to ${OBJ_PATH}"
			exit
		fi
	fi

	tar -xvf ${BUSYBOX}	

	if [ "$ARCH" = "riscv64" -o "$ARCH" = "all" ]; then
		cd ${BUSYBOX_DIR}
		cp ${RISCV64_CONFIG} ./.config
		make -j32
		if [ $? -ne 0 ]; then
			echo "compile error use ${BUSYBOX}, try use local busybox ${LOCAL_BUSYBOX}..."
			cd -
			tar -xvf ${LOCAL_BUSYBOX}
			cd ${BUSYBOX_DIR}
			cp ${RISCV64_CONFIG} ./.config
			make -j32 || return 1
		fi
		make install || return 1
		init_filesystem

		mkdir -p ${INSTALL_PATH}/riscv64
		cp _install -r ${INSTALL_PATH}/riscv64 || return 1
		make clean
	fi

	if [ "$ARCH" = "aarch64" -o "$ARCH" = "all" ]; then
		cd ${BUSYBOX_DIR}
		cp ${AARCH64_CONFIG} ./.config
		make -j32
		if [ $? -ne 0 ]; then
			echo "compile error use ${BUSYBOX}, try use local busybox ${LOCAL_BUSYBOX}..."
			cd -
			tar -xvf ${LOCAL_BUSYBOX}
			cd ${BUSYBOX_DIR}
			cp ${RISCV64_CONFIG} ./.config
			make -j32 || return 1
		fi
		make install || return 1
		init_filesystem

		mkdir -p ${INSTALL_PATH}/aarch64
		cp _install -r ${INSTALL_PATH}/aarch64 || return 1
		make clean
		cd ..
	fi
}

#
# 3. init start file
#
init_start_file()
{
	arch=$1

	cd ${INSTALL_PATH}/${arch}

	echo "cd _install; find . | cpio -o -H newc |gzip > ../rootfs.cpio.gz; cd .." > qemu
	echo "" >> qemu

	echo "${QEMU_INSTALL}/bin/qemu-system-${arch} \\" >> qemu

	if [ "$arch" = "riscv64" -o "$arch" = "all" ]; then
		cat ${OBJ_PATH}/src/qemu_riscv64 >> qemu
	elif [ "$arch" = "aarch64" ]; then
		cat ${OBJ_PATH}/src/qemu_aarch64 >> qemu
	fi

	chmod 755 qemu

	echo "${arch}_qemu install succ, run it: cd ${INSTALL_PATH}/${arch}; ./qemu"
}

args_help()
{
	echo "./QemuInstall [option]"
	echo "	opthin:"
	echo "		aarch64: Install aarch64 qemu"
	echo "		riscv64: Install riscv64 qemu"
	echo "		all: Install aarch64 and riscv64 qemu"
}

if [ "$ARCH" != "riscv64" -a "$ARCH" != "aarch64"  -a "$ARCH" != "all" ]; then
	args_help
	exit 1
fi

if [ -d ${QEMU_INSTALL} ]; then
	echo "find ${QEMU_INSTALL}, skip install qemu..."
else
	qemu_install
	if [ $? -ne 0 ]; then
		echo "qemu install fail"
		exit 1
	fi
fi

if [ -d ${INSTALL_PATH} ]; then
	echo "find ${INSTALL_PATH}, skip install busybox..."
else
	busybox_install
	if [ $? -ne 0 ]; then
		echo "busybox install fail"
		exit 1
	fi
fi

if [ "$ARCH" = "riscv64" -o "$ARCH" = "all" ]; then
	init_start_file riscv64
fi
if [ "$ARCH" = "aarch64" -o "$ARCH" = "all" ]; then
	init_start_file aarch64
fi
